package cn.topcodes.tcsf.cms.service;

import cn.topcodes.tcsf.cms.domain.entity.ArticleCatalog;
import cn.topcodes.tcsf.admin.commons.generic.GenericService;
import cn.topcodes.tcsf.cms.domain.vo.NestableSort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 文章分类 服务
 *
 * @author Unicorn
 * @date 2017-12-25 21:34:53
 */
@Service
public class ArticleCatalogService extends GenericService<ArticleCatalog> {

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void saveSort(List<NestableSort> sort) {
        tranSaveSort(sort, null);
    }

    private void tranSaveSort(List<NestableSort> sort, Long parentId) {
        if (sort == null) return;
        for (int i = 0; i < sort.size(); i++) {
            NestableSort s = sort.get(i);
            ArticleCatalog helpArticleCatalog = this.findOne(s.getId());
            helpArticleCatalog.setSort(i);
            helpArticleCatalog.setParentId(parentId);
            this.update(helpArticleCatalog);
            tranSaveSort(s.getChildren(), s.getId());
        }
    }
}
